﻿using System.ComponentModel;
using System.Windows;
using ChatClient.Services;
using ChatClient.ViewModels;
using DotnetUtil.Services;
using Prism.Ioc;
using Prism.Regions;


namespace ChatClient.Views
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    // ReSharper disable once RedundantExtendsListEntry
    public partial class LoginWindow : Window
    {
        private readonly IContainerExtension _container;
        private readonly IRegionManager _regionManager;

        public LoginWindow(
            ILogService logService,
            IChatService chatService,
            IDialogService dialogService,
            ISessionService sessionService,
            IContainerExtension container,
            IRegionManager regionManager
        )
        {
            InitializeComponent();

            _container = container;
            _regionManager = regionManager;

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                var viewModel = new LoginWindowViewModel(
                    logService,
                    chatService,
                    dialogService,
                    sessionService
                );
                viewModel.CloseWindow += OnCloseWindow;
                viewModel.LoginSuccess += OnLoginSuccess;
                DataContext = viewModel;
            }
        }

        private void OnCloseWindow()
        {
            Close();
        }

        private void OnLoginSuccess()
        {
            var mainWindow = _container.Resolve<MainWindow>();
            RegionManager.SetRegionManager(mainWindow, _regionManager);
            mainWindow.Show();
            Close();
        }
    }
}
